package easy;

import util.PrintUtils;

import java.util.Arrays;

public class Solution_1652 {
    public int[] decrypt(int[] code, int k) {
        int[] array = new int[code.length];
        if (k == 0) {
            Arrays.fill(array, 0);
        } else {
            int[] sums = new int[code.length];
            sums[0] = code[0];
            for (int i = 1; i < code.length; i++) {
                sums[i] = sums[i - 1] + code[i];
            }

            for (int i = 0; i < code.length; i++) {
                int l, r, val;
                if (k > 0) {
                    l = (i + 1) % code.length;
                    r = (i + k) % code.length;
                } else {
                    l = (code.length + i + k) % code.length;
                    r = (code.length + i - 1) % code.length;
                }
                if (l <= r) {
                    val = sums[r] - sums[l] + code[l];
                } else {
                    val = sums[r] + sums[code.length - 1] - sums[l] + code[l];
                }
                array[i] = val;
            }
        }
        return array;
    }

    public static void main(String[] args) {
        Solution_1652 model = new Solution_1652();
        PrintUtils.arrayInt(model.decrypt(new int[]{10, 5, 7, 7, 3, 2, 10, 3, 6, 9, 1, 6}, -4));
//        PrintUtils.arrayInt(model.decrypt(new int[]{5, 7, 1, 4}, 3));
//        PrintUtils.arrayInt(model.decrypt(new int[]{1,2,3,4}, 0));
//        PrintUtils.arrayInt(model.decrypt(new int[]{2,4,9,3}, -2));
    }
}
